 
use std::sync::Arc;
use super::types::{IStreamReader, IStreamWriter};
use super::keyword_impl::StreamsImplement;


/// 这是最终提供给其他插件的上下文 API
pub trait StreamsContext {
    fn get_stream_reader(&self, stream_name: &str) -> Option<Box<dyn IStreamReader>>;
    fn get_stream_writer(&self, stream_name: &str) -> Option<Box<dyn IStreamWriter>>;
}

pub struct StreamsContextImpl {
    streams_impl: Arc<StreamsImplement>,
    contributor: String,
}

impl StreamsContextImpl {
    pub fn new(streams_impl: Arc<StreamsImplement>, contributor: &str) -> Self {
        Self {
            streams_impl,
            contributor: contributor.to_string(),
        }
    }
}

impl StreamsContext for StreamsContextImpl {
    fn get_stream_reader(&self, stream_name: &str) -> Option<Box<dyn IStreamReader>> {
        // TODO: 检查此 contributor 是否有权限读取此 stream
        self.streams_impl.get_stream(stream_name).map(|s| s.add_reader())
    }

    fn get_stream_writer(&self, stream_name: &str) -> Option<Box<dyn IStreamWriter>> {
        // TODO: 检查此 contributor 是否有权限写入此 stream
        self.streams_impl.get_stream(stream_name).map(|s| s.add_writer())
    }
}